package com.hugang.date;

import org.junit.Test;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.temporal.TemporalAccessor;
import java.util.Date;

/**
 * JDK8中新增的日期时间API
 *
 * @author hg
 * @date 2020/6/17 16:55
 */
public class JDK8DateTimeTest {

    /**
     * LocalDate、LocalTime、LocalDateTime类的使用
     *   1、LocalDateTime使用频率相对高一些
     *   2、类似于Calendar
     */
    @Test
    public void test1() {
        //now(): 获取当前日期和时间
        LocalDate now = LocalDate.now();
        LocalTime now1 = LocalTime.now();
        LocalDateTime now2 = LocalDateTime.now();

        System.out.println(now);
        System.out.println(now1);
        System.out.println(now2);

        //of(): 设置指定的年月日时分秒，没有偏移量（new Date(2020, 3, 15, 22, 14, 22)有偏移量）
        Date date = new Date(2020, 3, 15, 22, 14, 22);
        LocalDateTime of = LocalDateTime.of(2020, 3, 15, 22, 14, 22);
        System.out.println(date);//Thu Apr 15 22:14:22 CST 3920
        System.out.println(of);

        //getXxx(): 获取相关属性
        System.out.println(now2.getDayOfMonth());
        System.out.println(now2.getDayOfWeek());
        System.out.println(now2.getDayOfYear());
        System.out.println(now2.getMonth());
        System.out.println(now2.getMonthValue());
        System.out.println(now2.getMinute());

        //withXxx(): 设置相关属性,体现不可变性
        LocalDateTime localDateTime = now2.withDayOfMonth(19);
        System.out.println(localDateTime);
        LocalDateTime localDateTime1 = now2.withHour(4);
        System.out.println(localDateTime1);

        //加减日期
        LocalDateTime localDateTime2 = now2.plusMonths(2);
        System.out.println(localDateTime2);
        LocalDateTime localDateTime3 = now2.minusMonths(2);
        System.out.println(localDateTime3);
    }

    /**
     * Instant的使用，类似于java.util.Date类
     */
    @Test
    public void test2(){
        //now()：获取本初子午线对应的标准时间
        Instant now = Instant.now();
        System.out.println(now);

        //atOffset(ZoneOffset offset): 添加时间的偏移量
        OffsetDateTime offsetDateTime = now.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);

        //toEpochMilli(): 获取对应的毫秒数
        long l = now.toEpochMilli();
        System.out.println(l);

        //ofEpochMilli(): 通过毫秒数构造对象
        Instant instant = Instant.ofEpochMilli(1592386097038L);
        System.out.println(instant);
    }

    /**
     * DateTimeFormatter类：格式化或解析日期、时间，类似于SimpleDateFormat;
     */
    @Test
    public void test3(){
        LocalDateTime localDateTime = LocalDateTime.now();
        //方式一：预定义的标准格式：
        DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        System.out.println(formatter.format(localDateTime));

        //解析：
        TemporalAccessor parse = formatter.parse("2020-06-17T18:11:55.17");
        System.out.println(parse);

        //方式二：本地化相关的格式，如：ofLocalizedDateTime(),参数：FormatStyle.LONG/MEDIUM/SHORT
        DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
        String format = formatter1.format(localDateTime);
        System.out.println(format);

        //方式三：自定义的格式
        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String format1 = formatter2.format(localDateTime);
        System.out.println(format1);



    }
}


